﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier_Interface;

namespace Cashier.purchase
{
    public partial class UpdateGoodsNumbersForm : Form
    {
        private IGoodsNumber iGoodsNumber;
        private int rowIndex = 0;
        private int goodsNumbers = 0;
        private int limitNumbers = 0;
        private int oldGoodsNumbers = 0;
        private bool isUpdate = false;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="iGoodsNumber">IGoodsNumber接口实例</param>
        /// <param name="rowindex">行的索引</param>
        /// <param name="barcode">商品条码</param>
        /// <param name="goodsName">商品名称</param>
        /// <param name="goodsNorm">商品规格</param>
        /// <param name="numbers">商品数量</param>
        /// <param name="limitNumbers">限制数量</param>
        public UpdateGoodsNumbersForm(IGoodsNumber iGoodsNumber, int rowindex, String barcode, String goodsName, 
            String goodsNorm, int numbers, int limitNumbers)
        {
            InitializeComponent();

            this.iGoodsNumber = iGoodsNumber;
            this.rowIndex = rowindex;
            this.label_barcode.Text = barcode;
            this.label_goodsName.Text = goodsName;
            this.label_goodsNorm.Text = goodsNorm;
            this.textBox_numbers.Text = numbers.ToString();
            this.oldGoodsNumbers = numbers;
            this.limitNumbers = limitNumbers;
        }

        /// <summary>
        /// 键盘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_numbers_KeyPress(object sender, KeyPressEventArgs e)
        {
            int keyValue = (int)e.KeyChar;

            if (!((keyValue > 47 && keyValue < 58) || keyValue == 8 || keyValue == 46))
                e.Handled = true;
            else
                e.Handled = false;
        }

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Escape:
                    isUpdate = false;
                    this.Close();
                    break;
                case Keys.Enter:
                    if (!this.button_ok.Focused)
                    {
                        isUpdate = true;
                        this.Close();
                    }
                    break;
            }
            return b;
        }

        /// <summary>
        /// 窗口关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateGoodsNumbersForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isUpdate)
            {
                int newGoodsNumebrs = Int32.Parse(this.textBox_numbers.Text.Trim());
                if (newGoodsNumebrs > 0 && newGoodsNumebrs != oldGoodsNumbers)
                {
                    if (limitNumbers > 0 && newGoodsNumebrs > limitNumbers)
                    {
                        MessageBox.Show("数量不得超过" + limitNumbers + "件");
                        e.Cancel = true;
                    }
                    else
                    {
                        this.iGoodsNumber.UpdateGoodsNumber(this.rowIndex, null, 0, newGoodsNumebrs);
                    }
                }
            }
        }

        /// <summary>
        /// 确定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_ok_Click(object sender, EventArgs e)
        {
            this.isUpdate = true;
            this.Close();
        }

        /// <summary>
        /// 取消事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.isUpdate = false;
            this.Close();
        }
    }
}
